package com.wanjia.estate.service.bdservice.zjsbdcjysbdservice;

import com.spire.xls.ExcelPicture;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.wanjia.estate.pojo.entity.Zjsbdcjysbd;
import com.wanjia.estate.utils.BarcodeGeneratorUtil;
import com.wanjia.estate.utils.Base64Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;

//Excel表格添加图片
@Slf4j
@Service
public class ExcelAddImgZjsbdcjysbdService {

    @Value("${transparentpath}")
    private String transparent;

    public String ExcelAddImg(String excelPath, Zjsbdcjysbd zjsbdcjysbd) throws IOException {



        File file = new File(excelPath);
        String absolutePath = file.getAbsolutePath();
        String name = absolutePath.split("\\.")[0];
        BarcodeGeneratorUtil.getBarCode(zjsbdcjysbd.getYwbh(),name+zjsbdcjysbd.getYwbh()+".png");//生成条形码

        //创建Workbook实例
        Workbook workbook = new Workbook();
        //加载Excel文档
        workbook.loadFromFile(excelPath);
        //获取第一张工作表
        Worksheet sheet = workbook.getWorksheets().get(0);

        //添加图片到工作表的指定位置
        ExcelPicture pic1 = sheet.getPictures().add(2, 9,name+zjsbdcjysbd.getYwbh()+".png");

        if (zjsbdcjysbd.getZrfqz()!=null){
            Base64Util.stringToImage(zjsbdcjysbd.getZrfqz(), name+"qm1");//生成签名
            ExcelPicture pic2 = sheet.getPictures().add(45, 3,name+"qm1.png");
            pic2.setWidth(75);
            pic2.setHeight(30);
        }else{
            ExcelPicture pic2 = sheet.getPictures().add(45, 3,transparent);
            pic2.setWidth(50);
            pic2.setHeight(50);
        }

        if (zjsbdcjysbd.getSrfqz()!=null){
            Base64Util.stringToImage(zjsbdcjysbd.getSrfqz(), name+"qm2");//生成签名
            ExcelPicture pic3 = sheet.getPictures().add(45, 11,name+"qm2.png");
            pic3.setWidth(75);
            pic3.setHeight(30);
        }else{
            ExcelPicture pic3 = sheet.getPictures().add(45, 11,transparent);
            pic3.setWidth(50);
            pic3.setHeight(50);
        }

        if (zjsbdcjysbd.getZrfdlrqz()!=null){
            Base64Util.stringToImage(zjsbdcjysbd.getZrfdlrqz(), name+"qm3");//生成签名
            ExcelPicture pic4 = sheet.getPictures().add(46, 3,name+"qm3.png");
            pic4.setWidth(75);
            pic4.setHeight(30);
        }else{
            ExcelPicture pic4 = sheet.getPictures().add(46, 3,transparent);
            pic4.setWidth(50);
            pic4.setHeight(50);
        }

        if (zjsbdcjysbd.getSrfdlrqz()!=null){
            Base64Util.stringToImage(zjsbdcjysbd.getSrfdlrqz(), name+"qm4");//生成签名
            ExcelPicture pic5 = sheet.getPictures().add(46, 11,name+"qm4.png");
            pic5.setWidth(75);
            pic5.setHeight(30);
        }else{
            ExcelPicture pic5 = sheet.getPictures().add(46, 11,transparent);
            pic5.setWidth(50);
            pic5.setHeight(50);
        }

        //设置图片的宽度和高度
        pic1.setWidth(200);
        pic1.setHeight(40);

        //保存文档
        workbook.saveToFile(excelPath, ExcelVersion.Version2013);

        return absolutePath;
    }
}
